package cc.aiee.chinapay.config;

import cc.aiee.core.bean.PayBaseConfig;
import cc.aiee.core.interfaces.PayJsonBeanInterface;
import cc.aiee.core.kit.KitJson;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ChinaPayConfig extends PayBaseConfig implements PayJsonBeanInterface {

	/**
	 * 商户私钥
	 */
	private String verifyFile;
	/**
	 * 交易证书路径
	 */
	private String signFile;

	/**
	 * 交易证书密码
	 */
	private String signFilePassword;
	/**
	 * 交易证书目录
	 */
	private String signFilePath;

	private String merchantId;

	private String merBgUrl;

	private String merBgRefundUrl;

	private String signCertType = "PKCS12";

	private String signInvalidFields = "Signature,CertId";

	private String signatureField = "Signature";

	public String getSignFilePath() {
		if (signFilePath == null || StrUtil.isBlank(signFilePath)) {
			Assert.notBlank(this.signFile, "sign file not NULL,交易证书路径不能为空");
			int lastIndexOf = signFile.lastIndexOf("/");
			if (lastIndexOf == -1) {
				lastIndexOf = signFile.lastIndexOf("\\");
			}
			if (lastIndexOf == -1) {
				return signFilePath;
			}
			signFilePath = this.signFile.substring(0, lastIndexOf);
		}
		return signFilePath;
	}

	@Override
	public String toJson() {
		return KitJson.toJSONString(this);
	}

	@Override
	public String toJson(boolean prettyFormat, boolean ignoreNull) {
		return KitJson.toJSONString(this, prettyFormat, ignoreNull);
	}

}
